//
//  AttackCommand.m
//  INFSWA02
//
//  Created by Justin van Grootveld on 10/26/11.
//  Copyright (c) 2011 Pro4all. All rights reserved.
//

#import "AttackCommand.h"
#import "Square.h"
#import "Board.h"
#import "Piece.h"

@implementation AttackCommand

- (BOOL)execute
{
    Board *board = [super board];
    
    if(!_squareSource || !_squareDestination || !board) return NO;
    
    if ([_squareSource isInAttackRangeOfSquareWithPiece:_squareDestination])
    {
        if([[_squareDestination piece] recieveDamage:[[_squareSource piece] physicalAttackPower]])
        {
            _piece = [_squareDestination piece]; //save piece separate for later undo.
            [_squareDestination removePiece];
        }
        
        return YES;
    }
    
    return NO;
}

- (BOOL)undo
{
    Board *board = [super board];
    
    if(!_squareSource || !_squareDestination || !board) return NO;
    
    //if piece is present, it died at the attack.
    if(_piece)  [_squareDestination setPiece:_piece];
        
    [[_squareDestination piece] reduceDamage:[[_squareSource piece] physicalAttackPower]];
    
    [board setCurrentState:[board noState]];
    
    return YES;
}

@end
